package common.pie.utils;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;


import com.liu.resume.R;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import common.constants.GConstant;

/**
 * 类、方法注释 参数加工类
 * @author creat  by:yts 2016/9/13
 * @author update by:
 *
 */
public class ParamsFactory {
    /**
     * 传递进来的post参数进行 加工
     */
    public static Map<String, String> processParams(Map<String, String> map) {
        //添加应用唯一标识
        map.put(GConstant.APP_ID, GConstant.APP_ID_VALUES);
        HashMap<String, String> disposeMap = new HashMap<>();
        Set<Map.Entry<String, String>> set = map.entrySet();
        // 遍历键值对对象的集合，得到每一个键值对对象
        for (Map.Entry<String, String> me : set) {
            // 根据键值对对象获取键和值，添加到请求参数Params
            disposeMap.put(me.getKey(), me.getValue());
            Log.e(me.getKey(), me.getValue());
        }
        //加密需要的参数，并添加到请求参数Params
        disposeMap.put(GConstant.SIGN, MD5Util.signMD5(map));
        Log.e(GConstant.SIGN, MD5Util.signMD5(map));
        return disposeMap;
    }

    //携带系统参数token的 参数加工
    public static Map<String, String> processWithToken(Context mContext, Map<String, String> map) {
        //获取保存的token和openid
        String access_token = (String) SharedPreferencesUtil.get(mContext, mContext.getString(R.string.access_token), "");
        int openid = (int) SharedPreferencesUtil.get(mContext, mContext.getString(R.string.openid), 0);
        if (TextUtils.isEmpty(access_token) || openid == 0) {
            Toast.makeText(mContext, "尚未登录", Toast.LENGTH_SHORT).show();
            return null;
        } else {
            map.put(GConstant.ACCESS_TOKEN, access_token);
            map.put(GConstant.OPEN_ID, openid + "");
            return processParams(map);
        }
    }
}